/*
 *  JWUtilities.c
 *  KotH
 *
 *  Created by James Weatherley on 10/08/2008.
 *  Copyright 2008 James Weatherley. All rights reserved.
 *
 */

#include "JWUtilities.h"
#include <limits.h>
#include <stdlib.h>
#include <math.h>

#import "JWSquare.h"

int max(int a, int b)
{
	return a > b ? a : b;
}

int randomInt(int max)
{
	double r = (double)random() / INT_MAX;
	r *= max;
	
	return r;
}

double angle(double x1, double z1, double x2, double z2)
{
	double r1 = sqrt(x1 * x1 + z1 * z1);
	double r2 = sqrt(x2 * x2 + z2 * z2);
	
	x1 /= r1;
	z1 /= r1;
	x2 /= r2;
	z2 /= r2;
	
	return acos(x1 * x2 + z1 * z2) * 180.0 / M_PI;
}

/*
void sphericalVector(JWSquare* from, JWSquare* to, JWSphericalVector* vec)
{
	vec->r = 0.0;
	vec->theta = 0.0;
	vec->phi = 0.0;
	
	double dx = [to x] - [from x];
	double dz = [to z] - [from z];
	double dh = [to height] - [from height];
	
	double r = sqrt(dx * dx + dz * dz);
	double r2 = sqrt(r * r + dh * dh);
	
	if(r) {
		vec->theta = acos(dz / r) * 180.0 / M_PI;
		if(dx < 0) {
			vec->theta = 360.0 - vec->theta;
		}
	}
	
	if(r2) {
		vec->phi = acos(r / r2) * 180.0 / M_PI;
	}
	vec->r = r2;
}
*/